/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.common.constant.ConfigInfo
 * <p>
 * 类产生时间: 2014-09-15 10:10
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 项目组 : 苏州公安移动门户
 * <p>
 * 部门: 手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.common.constant;

import com.oceansoft.mobile.econsole.common.util.encrypt.DESUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 系统配置属性加载类
 *
 * @author chenw
 * @time 2014-09-15 10:10
 */
@Component(value = "configInfo")
public class ConfigInfo {

    public static final int DEFAULT_HOST_PORT = 80;

    @Value("#{configReader['jdbc.driver']}")
    private String jdbcDriver;

    @Value("#{configReader['jdbc.url']}")
    private String jdbcURL;

    @Value("#{configReader['jdbc.username']}")
    private String jdbcUserName;

    @Value("#{configReader['jdbc.password']}")
    private String jdbcPassword;

    @Value("#{configReader['redis.host']}")
    private String redisServerHost;

    @Value("#{configReader['redis.port']}")
    private String redisServerPort;

    @Value("#{configReader['redis.password']}")
    private String redisPassword;

    @Value("#{configReader['server.app.host']}")
    private String appServerHost;

    @Value("#{configReader['server.app.port']}")
    private String appServerPort;

    @Value("#{configReader['server.ga.host']}")
    private String gaServerHost;

    @Value("#{configReader['server.ga.port']}")
    private String gaServerPort;

    @Value("#{configReader['server.push.host']}")
    private String pushServerHost;

    @Value("#{configReader['server.push.port']}")
    private String pushServerPort;

    @Value("#{configReader['server.message.wxtoken']}")
    private String wxAccessToken;

    @Value("#{configReader['server.message.wechat']}")
    private String wechatSendUrl;

    @Value("#{configReader['server.message.sms']}")
    private String smsSendUrl;

    @Value("#{configReader['service.interface.address']}")
    private String serviceInterfaceAddress;

    @Value("#{configReader['server.message.app']}")
    private String appSendUrl;

    @Value("#{configReader['server.file.host']}")
    private String fileServerHost;

    @Value("#{configReader['server.file.port']}")
    private String fileServerPort;

    @Value("#{configReader['server.file.username']}")
    private String fileServerUsername;

    @Value("#{configReader['server.file.password']}")
    private String fileServerPassword;

    @Value("#{configReader['server.file.path.complaint']}")
    private String fileServerPathComplaint;

    @Value("#{configReader['file.upload.root.path']}")
    private String fileUploadRootPath;

    @Value("#{configReader['file.download.mapping.path']}")
    private String fileDownloadMappingPath;

    @Value("#{configReader['smb.sharefolder']}")
    private String smbShareFolder;

    @Value("#{configReader['smb.server.host']}")
    private String smbServerHost;

    @Value("#{configReader['smb.username']}")
    private String smbUserName;

    @Value("#{configReader['smb.password']}")
    private String smbPassword;

    @Value("#{configReader['file.dir.biz']}")
    private String bizDir;

    @Value("#{configReader['file.dir.news']}")
    private String newsDir;

    @Value("#{configReader['file.dir.icon']}")
    private String iconDir;

    @Value("#{configReader['file.dir.plugins']}")
    private String pluginDir;

    @Value("#{configReader['file.dir.traffice']}")
    private String trafficeDir;

    @Value("#{configReader['file.dir.complaint']}")
    private String complaintDir;

    @Value("#{configReader['file.dir.movecar']}")
    private String moveCarDir;

    @Value("#{configReader['server.jlgasp.host']}")
    private String jlgaHost;

    @Value("#{configReader['server.ga.menhu.host']}")
    private String gamenhuServerHost;

    @Value("#{configReader['server.ga.menhu.port']}")
    private String gamenhuServerPort;

    @Value("#{configReader['server.ga.dating.host']}")
    private String gadatingServerHost;

    //-------------------------业务系统配置-----------------------
    @Value("#{configReader['biz.url.weather-report']}")
    private String weatherReportUrl;

    @Value("#{configReader['biz.accident.accept.start']}")
    private String accidentAcceptStart;

    @Value("#{configReader['biz.accident.accept.end']}")
    private String accidentAcceptEnd;

    @Value("#{configReader['biz.accident.sms.confirm']}")
    private String accidentConfirmSMS;

    @Value("#{configReader['biz.accident.sms.baseinfo']}")
    private String accidentBaseInfo;

    @Value("#{configReader['biz.accident.sms.unconfirm']}")
    private String accidentUnConfirm;

    @Value("#{configReader['biz.CaseNo']}")
    private String jgCaseNo;


    //-----------图片压缩路径---------------------//
    @Value("#{configReader['file.upload']}")
    private String fileUpload;

    @Value("#{configReader['work.flow.url']}")
    private String workFlowUrl;


    //------------DCN---------------------------//
    @Value("#{configReader['dcn.url']}")
    private String dcnUrl;
    @Value("#{configReader['dcn.user']}")
    private String dcnUser;
    @Value("#{configReader['dcn.pwd']}")
    private String dcnPwd;
    @Value("#{configReader['dcn.ntf_local_host']}")
    private String ntfLocalHost;
    @Value("#{configReader['dcn.cmd.del']}")
    private String cmdDel;
    @Value("#{configReader['dcn.cmd.blacklist']}")
    private String dcnBlackList;


    @Value("#{configReader['dcn.plugins.package']}")
    private String pluginsPackage;

    @Value("#{configReader['dcn.plugins.classes']}")
    private String pluginsName;

    @Value("#{configReader['server.message.media']}")
    private String weChatMediaSendUrl;

    @Value("#{configReader['weChat.news.url']}")
    private String apiNewsUrl;


    //===================================属性访问=============================

    public String getServiceInterfaceAddress(){
        return serviceInterfaceAddress;
    }

    public String getApiNewsUrl() {
        return apiNewsUrl;
    }

    public String getWorkFlowUrl() {
        return workFlowUrl;
    }
    public String getFileServerUsername() {
        return fileServerUsername;
    }

    public String getFileServerPassword() {
        return fileServerPassword;
    }

    public String getFileServerPathComplaint() {
        return fileServerPathComplaint;
    }

    public String getJgCaseNo() {
        return jgCaseNo;
    }

    public String getAppServerHost() {
        return appServerHost;
    }

    public String getAppServerPort() {
        return appServerPort;
    }

    public String getGaServerHost() {
        return gaServerHost;
    }

    public String getGaServerPort() {
        return gaServerPort;
    }

    public String getPushServerHost() {
        return pushServerHost;
    }

    public String getPushServerPort() {
        return pushServerPort;
    }

    public String getWxAccessToken() {
        return wxAccessToken;
    }

    public String getWechatSendUrl() {
        return wechatSendUrl;
    }

    public String getSmsSendUrl() {
        return smsSendUrl;
    }

    public String getAppSendUrl() {
        return appSendUrl;
    }

    public String getFileUpload() { return fileUpload; }

    public String getGamenhuServerHost() {
        return gamenhuServerHost;
    }


    public String getGadatingServerHost() {
        return gadatingServerHost;
    }


    public String getDcnUrl() {
        return dcnUrl;
    }

    public String getDcnUser() {
        return dcnUser;
    }

    public String getDcnPwd() {
        return dcnPwd;
    }

    public String getNtfLocalHost() {
        return ntfLocalHost;
    }

    public String getCmdDel() {
        return cmdDel;
    }

    public String getDcnBlackList() {
        return dcnBlackList;
    }

    public String getPluginsPackage() {
        return pluginsPackage;
    }

    public String getPluginsName() {
        return pluginsName;
    }

    public String getWeChatMediaSendUrl() {
        return weChatMediaSendUrl;
    }

    /**
     * 获取文件上传根目录如：D:/static/jlga/econsole/upload
     *
     * @return 文件上传根目录路径
     */
    public String getFileUploadRootPath() {
        return fileUploadRootPath;
    }

    /**
     * 获取文件上传路径(文件存储路径),如：D:/static/jlga/econsole/upload/{resourcePath}
     *
     * @param resourcePath 资源存储相对路径
     * @return String 资源在服务器存储绝对路径
     */
    public String getFileUploadFullPath(String resourcePath) {
        return String.format("%s/%s", fileUploadRootPath, resourcePath.endsWith("/") ? resourcePath.substring(1) : resourcePath);
    }

    public String getWeatherReportUrl() {
        return weatherReportUrl;
    }

    /**
     * 获取数据库登录用户名
     *
     * @return 数据库登录用户名明文
     */
    public String getJdbcUserName() {
        return getVal(jdbcUserName);
    }

    /**
     * 获取数据库登录密码
     *
     * @return 数据库登录密码明文
     */
    public String getJdbcPassword() {
        return getVal(jdbcPassword);
    }

    /**
     * 获取Redis登录密码
     *
     * @return 密码明文
     */
    public String getRedisPassword() {
        return getVal(redisPassword);
    }

    public String getRedisServerHost() {
        return getVal(redisServerHost);
    }

    public String getRedisServerPort() {
        return getVal(redisServerPort);
    }

    public String getJdbcDriver() {
        return getVal(jdbcDriver);
    }

    public String getJdbcURL() {
        return getVal(jdbcURL);
    }

    /**
     * 获取文件服务器主机地址<br>如：172.17.100.139
     *
     * @return String 服务器主机IP
     */
    public String getFileServerHost() {
        return fileServerHost;
    }

    public String getFileServerPort() {
        return fileServerPort;
    }

    public String getFileDownloadMappingPath() {
        return fileDownloadMappingPath;
    }

    public String getSmbServerHost() {
        return smbServerHost;
    }

    public String getSmbShareFolder() {
        return smbShareFolder;
    }

    public String getSmbUserName() {
        return getVal(smbUserName);
    }

    public String getSmbPassword() {
        return getVal(smbPassword);
    }

    public String getBizDir() {
        return bizDir;
    }

    public String getNewsDir() {
        return newsDir;
    }

    public String getIconDir() {
        return iconDir;
    }

    public String getPluginDir() {
        return pluginDir;
    }

    public String getTrafficeDir() {
        return trafficeDir;
    }

    public String getMoveCarDir() {
        return moveCarDir;
    }

    public String getComplaintDir() {
        return complaintDir;
    }

    public String getJlgaLogin() {
        return jlgaHost;
    }

    public int getAccidentAcceptStart() {
        if (StringUtils.hasText(accidentAcceptStart))
            return Integer.valueOf(accidentAcceptStart);
        return 0;
    }

    public int getAccidentAcceptEnd() {
        if (StringUtils.hasText(accidentAcceptEnd))
            return Integer.valueOf(accidentAcceptEnd);
        return 24;
    }

    public String getAccidentConfirmSMS(String... smsParams) {
        return String.format(accidentConfirmSMS, smsParams);
    }

    public String getAccidentBaseInfo(String... smsParams) {
        return String.format(accidentBaseInfo, smsParams);
    }

    public String getAccidentUnConfirm(String... smsParams) {
        return String.format(accidentUnConfirm, smsParams);
    }
    //---------------------扩展属性方法---------------------------

    /**
     * 获取公安接口资源地址
     *
     * @param resourcePath 资源路径
     * @return 完整资源地址
     */
    public String getGAApiUrl(String resourcePath, String... paramPair) {
        StringBuilder rt = new StringBuilder("http://").append(getGaServerHost()).append(":").append(getGaServerPort());
        if (StringUtils.hasText(resourcePath)) {
            if (resourcePath.startsWith("/")) {
                rt.append(resourcePath);
            } else {
                rt.append("/").append(resourcePath);
            }
        }
        int len = paramPair.length;
        if (len > 1 && 0 == len % 2) {
            for (int i = 0; i < paramPair.length; i++) {
                if (i == 0) {
                    rt.append("?");
                } else {
                    rt.append("&");
                }
                rt.append(paramPair[i]).append("=");
                i++;
                rt.append(paramPair[i]);
            }
        }
        return rt.toString();
    }

    private String getVal(String cryptStr) {
        if (Constant.MODEL_RELEASE && StringUtils.hasText(cryptStr)) {
            return DESUtil.decrypt(cryptStr);
        }
        return cryptStr;
    }

    public String getBizUrlWeatherReport() {
        return weatherReportUrl;
    }

    /**
     * 获取SMB共享根目录路径<br>如：sbm://admin:123@172.17.100.139/upload
     *
     * @return SMB共享目录路径
     */
    public String getSmbFolder() {
        return String.format("smb://%s:%s@%s/%s", getSmbUserName(), getSmbPassword(), getSmbServerHost(), getSmbShareFolder());
    }

    /**
     * 获取SMB共享目录路径下一级目录:<br>如：sbm://admin:123@172.17.100.139//upload/{parentDir}
     *
     * @param parentDir 文件父级目录
     * @return SMB共享目录路径
     */
    public String getSmbFolder(String parentDir) {
        return String.format("%s/%s", getSmbFolder(), parentDir);
    }

    /**
     * 获取共享目录中业务数据子目录<br>如：smb://admin:123@172.17.100.139/upload/biz/{userId}/20140918/{catalogName}
     *
     * @param userId      用户编号
     * @param catalogName 业务目录名称 traffice
     * @return 共享目录中业务数据子目录URL
     */
    public String getSmbBizDayDir(String userId, String catalogName) {
        return String.format("%s/%s", getSmbFolder(), getBizDayDirRelPath(userId, catalogName));
    }

    /**
     * 获取上传文件目录子目录<br>如：D:/static/jlga/econsole/upload/{subDirName}
     *
     * @param subDirName 一级目录名称 如：biz,icon,plugin,app等
     * @return 一级目录路径
     */
    public String getUploadSubDir(String subDirName) {
        return String.format("%s/%s", getFileUploadRootPath(), subDirName);
    }

    /**
     * 获取ICON图标上传目录
     *
     * @return 图标目录路径
     */
    public String getIconUploadDir() {
        return getUploadSubDir("icon");
    }

    /**
     * 获取应用图标下载路径：如：http://172.17.100.139:80/econsole/download/icon/{iconName}.ico
     *
     * @param iconName 应用图标名称
     * @return 应用图标下载路径
     */
    public String getIconUrl(String iconName) {
        return getFileDownloadUrl(getFileDownloadRootUrl() + "/icon/" + iconName);
    }

    /**
     * 获取文件服务器根目录下载Url<br> 如:http://172.17.100.139:80/econsole/download
     *
     * @return 文件下载目录根路径Url
     */
    public String getFileDownloadRootUrl() {
        return String.format("http://%s:%s/%s", getAppServerHost(), getAppServerPort(), getFileDownloadMappingPath());
    }

    /**
     * 获取资源文件下载路径 <br>如：http://172.17.100.139:80/econsole/download/{resourcePath}
     *
     * @param resourcePath 资源文件相对路径 eg: icon/abc.ico,  biz/20140918/traffice/abc.jpg
     * @return String 资源文件下载完整路径
     */
    public String getResourceDownloadUrl(String resourcePath) {
        return getFileDownloadRootUrl() + (resourcePath.startsWith("/") ? resourcePath : ("/" + resourcePath));
    }

    /**
     * 获取文件下载URL路径<br>如：http://172.17.100.139:80/econsole/download/{resource}
     *
     * @param resource 资源文件
     * @return 资源文件下载路径
     */
    public String getFileDownloadUrl(String resource) {
        if (StringUtils.hasText(resource)) {
            if (resource.startsWith("/"))
                return String.format("http://%s:%s/%s%s", getAppServerHost(), getAppServerPort(), getFileDownloadMappingPath(), resource);
            else
                return String.format("http://%s:%s/%s/%s", getAppServerHost(), getAppServerPort(), getFileDownloadMappingPath(), resource);
        } else {
            return "";
        }
    }

    /**
     * 获取业务文件存储日目录<br>如：D:/static/jlga/upload/biz/20140918/{bizDirName}
     *
     * @param userId     用户ID
     * @param bizDirName 业务子目录名称
     * @return File 目录绝对路径
     */
    public File getBizDayDir(String userId, String bizDirName) {
        String _pathStr = String.format("%s/biz/%s/%s/%s", getFileUploadRootPath(), userId, (new SimpleDateFormat("yyyyMMdd").format(new Date())), bizDirName);
        File _path = new File(_pathStr);
        if (_path.exists()) {
            if (!_path.isDirectory()) {
                _path.delete();
            } else {
                return _path;
            }
        } else {
            try {
                org.apache.commons.io.FileUtils.forceMkdir(_path);
            } catch (Exception ex) {
                return null;
            }
        }
        return _path;
    }

    /**
     * 获取业务文件存储日目录<br>如：biz/{userId}/20140918/{bizDirName}
     *
     * @param userId     用户ID
     * @param bizDirName 业务目录名称
     * @return 目录相对路径
     */
    public String getBizDayDirRelPath(String userId, String bizDirName) {
        return String.format("biz/%s/%s/%s", userId, (new SimpleDateFormat("yyyyMMdd").format(new Date())), bizDirName);
    }
}
